#include <i264e_common.h>
#ifdef DMALLOC
#include <imp/dmalloc.h>
#endif

#define I264E_RCFG_C2G_V		(1<<I264E_RCFG_C2G_ID)
#define I264E_RCFG_CUT_V		(1<<I264E_RCFG_CUT_ID)
#define I264E_RCFG_ROI_V		(1<<I264E_RCFG_ROI_ID)
#define I264E_RCFG_RC_V			(1<<I264E_RCFG_RC_ID)
#define I264E_RCFG_RC_TRIG_V	(1<<I264E_RCFG_RC_TRIG_ID)
#define I264E_RCFG_FPS_V		(1<<I264E_RCFG_FPS_ID)
#define I264E_RCFG_ENIDR_V		(1<<I264E_RCFG_ENIDR_ID)
#define I264E_RCFG_GOP_V		(1<<I264E_RCFG_GOP_ID)
#define I264E_RCFG_DN_V			(1<<I264E_RCFG_DN_ID)
#define I264E_RCFG_HSKIP_V		(1<<I264E_RCFG_HSKIP_ID)
#define I264E_RCFG_BENHANCE_V	(1<<I264E_RCFG_BENHANCE_ID)
#define I264E_RCFG_MBRC_V	    (1<<I264E_RCFG_MBRC_ID)
#define I264E_RCFG_CHGREF_V	    (1<<I264E_RCFG_CHGREF_ID)
#define I264E_RCFG_SUPER_V	    (1<<I264E_RCFG_SUPER_ID)
#define I264E_RCFG_TRANS_V	    (1<<I264E_RCFG_TRANS_ID)
#define I264E_RCFG_QPGMODE_V	(1<<I264E_RCFG_QPGMODE_ID)

#define I264E_RCFG_C2G_CLR(v) (v = (v&(~(I264E_RCFG_C2G_V))))
#define I264E_RCFG_CUT_CLR(v) (v = (v&(~(I264E_RCFG_CUT_V))))
#define I264E_RCFG_ROI_CLR(v) (v = (v&(~(I264E_RCFG_ROI_V))))
#define I264E_RCFG_RC_CLR(v)  (v = (v&(~(I264E_RCFG_RC_V))))
#define I264E_RCFG_RC_TRIG_CLR(v)  (v = (v&(~(I264E_RCFG_RC_TRIG_V))))
#define I264E_RCFG_FPS_CLR(v)  (v = (v&(~(I264E_RCFG_FPS_V))))
#define I264E_RCFG_ENIDR_CLR(v)  (v = (v&(~(I264E_RCFG_ENIDR_V))))
#define I264E_RCFG_GOP_CLR(v)  (v = (v&(~(I264E_RCFG_GOP_V))))
#define I264E_RCFG_DN_CLR(v)  (v = (v&(~(I264E_RCFG_DN_V))))
#define I264E_RCFG_HSKIP_CLR(v)  (v = (v&(~(I264E_RCFG_HSKIP_V))))
#define I264E_RCFG_BENHANCE_CLR(v)  (v = (v&(~(I264E_RCFG_BENHANCE_V))))
#define I264E_RCFG_MBRC_CLR(v)  (v = (v&(~(I264E_RCFG_MBRC_V))))
#define I264E_RCFG_CHGREF_CLR(v)  (v = (v&(~(I264E_RCFG_CHGREF_V))))
#define I264E_RCFG_SUPER_CLR(v)  (v = (v&(~(I264E_RCFG_SUPER_V))))
#define I264E_RCFG_TRANS_CLR(v)  (v = (v&(~(I264E_RCFG_TRANS_V))))
#define I264E_RCFG_QPGMODE_CLR(v)  (v = (v&(~(I264E_RCFG_QPGMODE_V))))

#define I264E_RCFG_C2G_SET(v) (v = (v|(I264E_RCFG_C2G_V)))
#define I264E_RCFG_CUT_SET(v) (v = (v|(I264E_RCFG_CUT_V)))
#define I264E_RCFG_ROI_SET(v) (v = (v|(I264E_RCFG_ROI_V)))
#define I264E_RCFG_RC_SET(v)  (v = (v|(I264E_RCFG_RC_V)))
#define I264E_RCFG_RC_TRIG_SET(v)  (v = (v|(I264E_RCFG_RC_TRIG_V)))
#define I264E_RCFG_FPS_SET(v)  (v = (v|(I264E_RCFG_FPS_V)))
#define I264E_RCFG_ENIDR_SET(v)  (v = (v|(I264E_RCFG_ENIDR_V)))
#define I264E_RCFG_GOP_SET(v)  (v = (v|(I264E_RCFG_GOP_V)))
#define I264E_RCFG_DN_SET(v)  (v = (v|(I264E_RCFG_DN_V)))
#define I264E_RCFG_HSKIP_SET(v)  (v = (v|(I264E_RCFG_HSKIP_V)))
#define I264E_RCFG_BENHANCE_SET(v)  (v = (v|(I264E_RCFG_BENHANCE_V)))
#define I264E_RCFG_MBRC_SET(v)  (v = (v|(I264E_RCFG_MBRC_V)))
#define I264E_RCFG_CHGREF_SET(v)  (v = (v|(I264E_RCFG_CHGREF_V)))
#define I264E_RCFG_SUPER_SET(v)  (v = (v|(I264E_RCFG_SUPER_V)))
#define I264E_RCFG_TRANS_SET(v)  (v = (v|(I264E_RCFG_TRANS_V)))
#define I264E_RCFG_QPGMODE_SET(v)  (v = (v|(I264E_RCFG_QPGMODE_V)))

#define I264E_RCFG_C2G_GET(v) (v&(I264E_RCFG_C2G_V))
#define I264E_RCFG_CUT_GET(v) (v&(I264E_RCFG_CUT_V))
#define I264E_RCFG_ROI_GET(v) (v&(I264E_RCFG_ROI_V))
#define I264E_RCFG_RC_GET(v)  (v&(I264E_RCFG_RC_V))
#define I264E_RCFG_RC_TRIG_GET(v)  (v&(I264E_RCFG_RC_TRIG_V))
#define I264E_RCFG_FPS_GET(v)  (v&(I264E_RCFG_FPS_V))
#define I264E_RCFG_ENIDR_GET(v)  (v&(I264E_RCFG_ENIDR_V))
#define I264E_RCFG_GOP_GET(v)  (v&(I264E_RCFG_GOP_V))
#define I264E_RCFG_DN_GET(v)  (v&(I264E_RCFG_DN_V))
#define I264E_RCFG_HSKIP_GET(v)  (v&(I264E_RCFG_HSKIP_V))
#define I264E_RCFG_BENHANCE_GET(v)  (v&(I264E_RCFG_BENHANCE_V))
#define I264E_RCFG_MBRC_GET(v)  (v&(I264E_RCFG_MBRC_V))
#define I264E_RCFG_CHGREF_GET(v)  (v&(I264E_RCFG_CHGREF_V))
#define I264E_RCFG_SUPER_GET(v)  (v&(I264E_RCFG_SUPER_V))
#define I264E_RCFG_TRANS_GET(v)  (v&(I264E_RCFG_TRANS_V))
#define I264E_RCFG_QPGMODE_GET(v)  (v&(I264E_RCFG_QPGMODE_V))

int i264e_reconfig_init(i264e_t *h)
{
	if (i264e_pthread_mutex_init(&h->common.reconfig.mutex_rcfg, NULL) < 0) {
		i264e_log(h, C_LOG_ERROR, "pthread_mutex_init failed\n");
		goto err_rcfg_mutex_init;
	}
	/* buckup for init param */
	memcpy(&h->common.reconfig.parambak, &h->common.param, sizeof(i264e_param_t));
	h->common.reconfig.gopsizebak = h->common.param.i_gop;
	/* for rc attr get */
	h->common.reconfig.rc.rc_method = h->common.param.rc.i_rc_method;
	h->common.reconfig.rc.qp = h->common.param.rc.i_qp_constant;
	h->common.reconfig.rc.qp_min = h->common.param.rc.i_qp_min;
	h->common.reconfig.rc.qp_max = h->common.param.rc.i_qp_max;
	h->common.reconfig.rc.bitrate = h->common.param.rc.i_bitrate;
	h->common.reconfig.rc.biaslvl = h->common.param.rc.i_biaslvl;
	h->common.reconfig.rc.qp_step = h->common.param.rc.i_qp_step;
	h->common.reconfig.rc.gop_qp_step = h->common.param.rc.i_gop_qp_step;
	h->common.reconfig.rc.adaptive_mode = h->common.param.rc.b_adaptive_mode;
	h->common.reconfig.rc.gop_relation = h->common.param.rc.b_gop_relation;
	h->common.reconfig.rc.change_pos = h->common.param.rc.i_change_pos;
	h->common.reconfig.rc.quality_level = h->common.param.rc.i_quality_level;
	h->common.reconfig.rc.trigger_level = h->common.param.rc.i_trigger_level;
	h->common.reconfig.rc.new_qp_max_thr = h->common.param.rc.i_new_qp_max_thr;
	h->common.reconfig.rc.static_time = h->common.param.rc.i_static_time;
	h->common.reconfig.rc.max_bitrate = h->common.param.rc.i_max_bitrate;

	/* for fps attr get */
	h->common.reconfig.i_fps_num = h->common.param.i_fps_num;
	h->common.reconfig.i_fps_den = h->common.param.i_fps_den;
	/* for c2g */
	h->common.reconfig.empty = NULL;
	/* for roi get */
	memcpy(h->common.reconfig.roi, h->common.param.roi, 8*sizeof(c_roi_t));
	h->common.reconfig.denoise = h->common.param.denoise;
	h->common.reconfig.hskip = h->common.param.hskip;
    /* for mb rc */
	h->common.reconfig.b_enable_mbrc = h->common.param.rc.b_enable_mbrc;
    /* for change ref */
	h->common.reconfig.b_enable_change_ref = h->common.param.rc.b_enable_change_ref;
    /* for super frame */
    memcpy(&h->common.reconfig.superfrm_param, &h->common.param.superFrm, sizeof(c_superfrm_param_t));

    /* for chroma_qp_offset in tranform */
	h->common.reconfig.i_chroma_qp_offset = h->common.param.analyse.i_chroma_qp_offset;
    /*for macro block */
    h->common.reconfig.qpgmode = h->common.eprc_t21.cfg.qpgMode;
	return 0;
err_rcfg_mutex_init:
	return -1;
}

int i264e_reconfig_deinit(i264e_t *h)
{
	i264e_pthread_mutex_destroy(&h->common.reconfig.mutex_rcfg);
	if (h->common.reconfig.empty) {
		h->common.param.ckfree(-1, h->common.reconfig.empty);
		h->common.reconfig.empty = NULL;
	}
	return 0;
}

int i264e_reconfig(i264e_t *h)
{
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	if (h->common.reconfig.enable_c2g) {
		h->cur->p.uvplane_bak = h->cur->p.fenc->plane[1];
		h->cur->p.fenc->plane[1] = h->common.reconfig.empty;
	}

	/* next frame include idr and p */
	if (h->common.reconfig.p_reconfig_cnt) {
		if (I264E_RCFG_ROI_GET(h->common.reconfig.p_reconfig_cnt)) {
			memcpy(h->cur->ip.roi, h->common.reconfig.roi, 8*sizeof(c_roi_t));
			I264E_RCFG_ROI_CLR(h->common.reconfig.p_reconfig_cnt);
		}
		if (I264E_RCFG_DN_GET(h->common.reconfig.p_reconfig_cnt)) {
			h->common.param.denoise = h->common.reconfig.denoise;
			if (!h->common.param.b_use_dn) {
				h->common.param.denoise.i_dn_type = I264E_DN_NONE;
			}
			I264E_RCFG_DN_CLR(h->common.reconfig.p_reconfig_cnt);
		}
		if (I264E_RCFG_BENHANCE_GET(h->common.reconfig.p_reconfig_cnt)) {
			h->common.param.hskip.b_black_enhance = !!h->common.reconfig.hskip.b_black_enhance;
			I264E_RCFG_BENHANCE_CLR(h->common.reconfig.p_reconfig_cnt);
		}
		if (I264E_RCFG_MBRC_GET(h->common.reconfig.p_reconfig_cnt)) {
			h->common.param.rc.b_enable_mbrc = !!h->common.reconfig.b_enable_mbrc;
			I264E_RCFG_MBRC_CLR(h->common.reconfig.p_reconfig_cnt);
		}
		if (I264E_RCFG_CHGREF_GET(h->common.reconfig.p_reconfig_cnt)) {
			h->common.param.rc.b_enable_change_ref = !!h->common.reconfig.b_enable_change_ref;
			I264E_RCFG_CHGREF_CLR(h->common.reconfig.p_reconfig_cnt);
		}
	}
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_idr_reconfig(i264e_t *h)
{
	int idr_reconfig_cnt = 0;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	idr_reconfig_cnt = h->common.reconfig.idr_reconfig_cnt;
	if (h->common.reconfig.idr_reconfig_cnt) {
		if (I264E_RCFG_C2G_GET(h->common.reconfig.idr_reconfig_cnt)) {
			if (h->common.reconfig.enable_c2g_cfg) {
				if (h->common.reconfig.empty == NULL) {
					h->common.reconfig.empty = h->common.param.ckmalloc(-1, C_ALIGN(h->common.param.i_width, 16) * C_ALIGN(h->common.param.i_height, 16) / 2, C_VB_ALIGN);
					if (h->common.reconfig.empty == NULL) {
						h->common.reconfig.enable_c2g = 0;
					} else {
						memset(h->common.reconfig.empty, 0x7f, C_ALIGN(h->common.param.i_width, 16) * C_ALIGN(h->common.param.i_height, 16) / 2);
						h->common.reconfig.enable_c2g = 1;
					}
				}
			} else {
				h->common.reconfig.enable_c2g = 0;
				if (h->common.reconfig.empty != NULL) {
					h->common.param.ckfree(-1, h->common.reconfig.empty);
					h->common.reconfig.empty = NULL;
				}
			}
			I264E_RCFG_C2G_CLR(h->common.reconfig.idr_reconfig_cnt);
		}
		if (I264E_RCFG_CUT_GET(h->common.reconfig.idr_reconfig_cnt)) {
			if (h->common.reconfig.enable_crop) {
				h->common.param.crop.i_left = h->common.reconfig.start_mb_x_cfg * 16;
				h->common.param.crop.i_top = h->common.reconfig.start_mb_y_cfg * 16;
				h->common.param.crop.i_right = h->common.param.i_width
					- h->common.param.crop.i_left - h->common.reconfig.mb_width * 16;
				h->common.param.crop.i_bottom = h->common.param.i_height
					- h->common.param.crop.i_top - h->common.reconfig.mb_height * 16;
			} else {
				h->common.param.crop.i_left = h->common.reconfig.parambak.crop.i_left;
				h->common.param.crop.i_top = h->common.reconfig.parambak.crop.i_top;
				h->common.param.crop.i_right = h->common.reconfig.parambak.crop.i_right;
				h->common.param.crop.i_bottom = h->common.reconfig.parambak.crop.i_bottom;
			}
			I264E_RCFG_CUT_CLR(h->common.reconfig.idr_reconfig_cnt);
		}
		if (I264E_RCFG_RC_GET(h->common.reconfig.idr_reconfig_cnt)) {
			h->common.param.rc.i_rc_method = h->common.reconfig.rc.rc_method;
			h->common.param.rc.i_qp_constant = h->common.reconfig.rc.qp;
			h->common.param.rc.i_bitrate = h->common.reconfig.rc.bitrate;
			h->common.param.rc.i_qp_min = h->common.reconfig.rc.qp_min;
			h->common.param.rc.i_qp_max = h->common.reconfig.rc.qp_max;
			h->common.param.rc.i_biaslvl = h->common.reconfig.rc.biaslvl;
			h->common.param.rc.i_qp_step = h->common.reconfig.rc.qp_step;
			h->common.param.rc.i_gop_qp_step = h->common.reconfig.rc.gop_qp_step;
			h->common.param.rc.b_adaptive_mode = h->common.reconfig.rc.adaptive_mode;
			h->common.param.rc.b_gop_relation = h->common.reconfig.rc.gop_relation;
			h->common.param.rc.i_static_time = h->common.reconfig.rc.static_time;
			h->common.param.rc.i_max_bitrate = h->common.reconfig.rc.max_bitrate;
			h->common.param.rc.i_change_pos = h->common.reconfig.rc.change_pos;
            h->common.param.rc.i_quality_level = h->common.reconfig.rc.quality_level;
			I264E_RCFG_RC_CLR(h->common.reconfig.idr_reconfig_cnt);
		}
		if (I264E_RCFG_RC_TRIG_GET(h->common.reconfig.idr_reconfig_cnt)) {
			h->common.param.rc.i_trigger_level = h->common.reconfig.rc.trigger_level;
			h->common.param.rc.i_new_qp_max_thr = h->common.reconfig.rc.new_qp_max_thr;
			I264E_RCFG_RC_TRIG_CLR(h->common.reconfig.idr_reconfig_cnt);
		}
		if (I264E_RCFG_FPS_GET(h->common.reconfig.idr_reconfig_cnt)) {
			h->common.param.i_fps_num = h->common.reconfig.i_fps_num;
			h->common.param.i_fps_den = h->common.reconfig.i_fps_den;
			h->common.param.i_timebase_den = h->common.reconfig.i_fps_num;
			h->common.param.i_timebase_num = h->common.reconfig.i_fps_den;
			I264E_RCFG_FPS_CLR(h->common.reconfig.idr_reconfig_cnt);
		}
		if (I264E_RCFG_ENIDR_GET(h->common.reconfig.idr_reconfig_cnt)) {
			if (h->common.reconfig.enable_enidr)
				h->common.param.i_gop = 1;
			else
				h->common.param.i_gop = h->common.reconfig.gopsizebak;
			I264E_RCFG_ENIDR_CLR(h->common.reconfig.idr_reconfig_cnt);
		}
		if (I264E_RCFG_GOP_GET(h->common.reconfig.idr_reconfig_cnt)) {
			if (h->common.reconfig.enable_enidr) {
				h->common.param.i_gop = 1;
			} else {
				h->common.param.i_gop = h->common.reconfig.gopsizebak;
			}
			I264E_RCFG_GOP_CLR(h->common.reconfig.idr_reconfig_cnt);
		}
		if (I264E_RCFG_HSKIP_GET(h->common.reconfig.idr_reconfig_cnt)) {
			h->common.param.hskip = h->common.reconfig.hskip;
			I264E_RCFG_HSKIP_CLR(h->common.reconfig.idr_reconfig_cnt);
		}
		if (I264E_RCFG_SUPER_GET(h->common.reconfig.idr_reconfig_cnt)) {
            memcpy(&h->common.param.superFrm, &h->common.reconfig.superfrm_param, sizeof(c_superfrm_param_t));
			I264E_RCFG_SUPER_CLR(h->common.reconfig.idr_reconfig_cnt);
		}
		if (I264E_RCFG_TRANS_GET(h->common.reconfig.idr_reconfig_cnt)) {
            memcpy(&h->common.param.analyse.i_chroma_qp_offset, &h->common.reconfig.i_chroma_qp_offset, sizeof(int));
			I264E_RCFG_TRANS_CLR(h->common.reconfig.idr_reconfig_cnt);
		}
		if (I264E_RCFG_QPGMODE_GET(h->common.reconfig.idr_reconfig_cnt)) {
			memcpy(&h->common.eprc_t21.cfg.qpgMode,&h->common.reconfig.qpgmode,sizeof(int));
			I264E_RCFG_QPGMODE_CLR(h->common.reconfig.idr_reconfig_cnt);
		}
	}
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	if ((idr_reconfig_cnt)) {
		h->common.param.i_level_idc = -1;
		int tmp_rdframe_cnt = 0, tmp_rdsframe_cnt = 0;
		int tmp_global_qp = h->cur->p.i_global_qp;
		i264e_validate_parameters(h);
		h->cur->p.fenc->b_force_idr = 1;
		//h->common.param.i_sps_id = (h->common.param.i_sps_id + 1) & 31;
		i264e_sps_init(h->common.sps, h->common.param.i_sps_id, &h->common.param);
		i264e_pps_init(h->common.pps, h->common.param.i_sps_id, &h->common.param, h->common.sps);
		i264e_validate_levels(h, 1);
		i264e_init_skip_header(&h->common.skip_h, h->common.param.i_gop, h->common.param.hskip.i_skip_type,
				h->common.param.hskip.m, h->common.param.hskip.n, h->common.param.hskip.max_same_scenecnt,
				h->common.param.i_max_skip_type, &tmp_rdframe_cnt, &tmp_rdsframe_cnt,
				h->common.param.b_mref, h->common.param.b_buf_share);
		i264e_ratecontrol_init(h, tmp_global_qp);
		i264e_decide_slice_type_and_rd(h, h->cur);
		i264e_ratecontrol_start(h->cur);
		i264e_decide_slice_qp(h, h->cur);
	}
	if (1 == h->common.reconfig.idr_rcfile_config_cnt) {
		h->common.reconfig.idr_rcfile_config_cnt = 0;
		h->common.param.i_level_idc = -1;
		int tmp_rdframe_cnt = 0, tmp_rdsframe_cnt = 0;
		int tmp_global_qp = h->cur->p.i_global_qp;
		i264e_validate_parameters(h);
		h->cur->p.fenc->b_force_idr = 1;
		//h->common.param.i_sps_id = (h->common.param.i_sps_id + 1) & 31;
		i264e_sps_init(h->common.sps, h->common.param.i_sps_id, &h->common.param);
		i264e_pps_init(h->common.pps, h->common.param.i_sps_id, &h->common.param, h->common.sps);
		i264e_validate_levels(h, 1);
		i264e_init_skip_header(&h->common.skip_h, h->common.param.i_gop, h->common.param.hskip.i_skip_type,
				h->common.param.hskip.m, h->common.param.hskip.n, h->common.param.hskip.max_same_scenecnt,
				h->common.param.i_max_skip_type, &tmp_rdframe_cnt, &tmp_rdsframe_cnt,
				h->common.param.b_mref, h->common.param.b_buf_share);
		i264e_ratecontrolfile_init(h, tmp_global_qp);
		i264e_decide_slice_type_and_rd(h, h->cur);
		i264e_ratecontrol_start(h->cur);
		i264e_decide_slice_qp(h, h->cur);
	}
	return 0;
}

int i264e_reconfig_color2gray_set(i264e_t *h, const void *param)
{
	const i264e_rcfg_c2g_param_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	I264E_RCFG_C2G_SET(h->common.reconfig.idr_reconfig_cnt);
	h->common.reconfig.enable_c2g_cfg = p->on;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_color2gray_get(i264e_t *h, void *param)
{
	i264e_rcfg_c2g_param_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	p->on = h->common.reconfig.enable_c2g_cfg;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_cut_set(i264e_t *h, const void *param)
{
	const i264e_rcfg_cut_param_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	if (p->on) {
		if ((p->mb_width<16)||(p->mb_height<16)) {
			i264e_log(h, C_LOG_ERROR, "reconfig param error! mb_width = %d mb_height = %d\n", p->mb_width, p->mb_height);
			i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
			return -1;
		}
		if (((p->start_mb_x+p->mb_width)>(C_ALIGN(h->common.param.i_width, 16)/16))||((p->start_mb_y+p->mb_height)>(C_ALIGN(h->common.param.i_height, 16)/16))) {
			i264e_log(h, C_LOG_ERROR, "reconfig param error! \n");
			i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
			return -1;
		}
	}
	h->common.reconfig.start_mb_x_cfg = p->start_mb_x;
	h->common.reconfig.start_mb_y_cfg = p->start_mb_y;
	h->common.reconfig.mb_width = p->mb_width;
	h->common.reconfig.mb_height = p->mb_height;
	h->common.reconfig.enable_crop = p->on;
	I264E_RCFG_CUT_SET(h->common.reconfig.idr_reconfig_cnt);
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_cut_get(i264e_t *h, void *param)
{
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	i264e_rcfg_cut_param_t *p = param;
	p->start_mb_x = h->common.reconfig.start_mb_x_cfg;
	p->start_mb_y = h->common.reconfig.start_mb_y_cfg;
	p->mb_width = h->common.reconfig.mb_width;
	p->mb_height = h->common.reconfig.mb_height;
	p->on = h->common.reconfig.enable_crop;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_roi_set(i264e_t *h, const void *param)
{
	const i264e_rcfg_roi_param_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	if (h->common.param.soc == C_M200) {
		i264e_log(h, C_LOG_ERROR, "m200 do not support roi! \n");
		i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
		return -1;
	}
	if ((p->idx < 0) || (p->idx > 8)) {
		i264e_log(h, C_LOG_ERROR, "reconfig param error! \n");
		i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
		return -1;
	}
	h->common.reconfig.roi[p->idx] = p->roi;
	I264E_RCFG_ROI_SET(h->common.reconfig.p_reconfig_cnt);
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_roi_get(i264e_t *h, void *param)
{
	i264e_rcfg_roi_param_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	if (h->common.param.soc == C_M200) {
		i264e_log(h, C_LOG_ERROR, "m200 do not support roi! \n");
		i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
		return -1;
	}
	if ((p->idx < 0) || (p->idx > 8)) {
		i264e_log(h, C_LOG_ERROR, "reconfig param error! \n");
		i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
		return -1;
	}
	p->roi = h->common.reconfig.roi[p->idx];
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_dn_set(i264e_t *h, const void *param)
{
	const i264e_denoise_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	if ((p->i_dn_type < I264E_DN_NONE) || (p->i_dn_type > I264E_DN_II)) {
		i264e_log(h, C_LOG_ERROR, "reconfig denoise param error! \n");
		i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
		return -1;
	}
	h->common.reconfig.denoise = *p;
	if (!h->common.param.b_use_dn) {
		h->common.reconfig.denoise.i_dn_type = I264E_DN_NONE;
	}
	I264E_RCFG_DN_SET(h->common.reconfig.p_reconfig_cnt);
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_dn_get(i264e_t *h, void *param)
{
	i264e_denoise_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	*p = h->common.reconfig.denoise;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_hskip_set(i264e_t *h, const void *param)
{
	const c_high_skip_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	if ((h->common.rdframe_cnt == 2) && (p->i_skip_type >= C_STYPE_N4X)) {
		i264e_log(h, C_LOG_ERROR, "reconfig hskip param error! \n");
		i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
		return -1;
	}
	h->common.reconfig.hskip = *p;
	I264E_RCFG_HSKIP_SET(h->common.reconfig.idr_reconfig_cnt);
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_hskip_get(i264e_t *h, void *param)
{
	c_high_skip_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	*p = h->common.reconfig.hskip;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_black_enhance_set(i264e_t *h, const void *param)
{
	const int *b_black_enhance = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	h->common.reconfig.hskip.b_black_enhance = !!(*b_black_enhance);
	I264E_RCFG_BENHANCE_SET(h->common.reconfig.p_reconfig_cnt);
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_mb_rc_get(i264e_t *h, void *param)
{
	int *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	*p = h->common.reconfig.b_enable_mbrc;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);

    return 0;
}

int i264e_mb_rc_set(i264e_t *h, const void *param)
{
	const int *b_enable_mbrc = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	h->common.reconfig.b_enable_mbrc = !!(*b_enable_mbrc);
	I264E_RCFG_MBRC_SET(h->common.reconfig.p_reconfig_cnt);
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);

    return 0;
}

int i264e_change_ref_get(i264e_t *h, void *param)
{
	int *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	*p = h->common.reconfig.b_enable_change_ref;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);

    return 0;
}

int i264e_change_ref_set(i264e_t *h, const void *param)
{
	const int *b_enable_change_ref = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	h->common.reconfig.b_enable_change_ref = !!(*b_enable_change_ref);
	I264E_RCFG_CHGREF_SET(h->common.reconfig.p_reconfig_cnt);
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);

    return 0;
}

int i264e_super_rc_get(i264e_t *h, void *param)
{
	c_superfrm_param_t *superfrm_param = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	memcpy(superfrm_param, &h->common.param.superFrm, sizeof(c_superfrm_param_t));
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);

    return 0;
}

int i264e_super_rc_set(i264e_t *h, const void *param)
{
	c_superfrm_param_t *superfrm_param = (c_superfrm_param_t *)param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	memcpy(&h->common.reconfig.superfrm_param, superfrm_param, sizeof(c_superfrm_param_t));
    h->common.reconfig.superfrm_param.mode = c_clip3(superfrm_param->mode, SF_DO_NOTHING, SF_RE_ENOCDE);
    h->common.reconfig.superfrm_param.priority = c_clip3(superfrm_param->mode, SF_PR_NOTHING, SF_FRAMEBITS_FIRST);
	I264E_RCFG_SUPER_SET(h->common.reconfig.idr_reconfig_cnt);
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);

    return 0;
}

int i264e_reconfig_trans_get(i264e_t *h, void *param)
{
    int *chroma_qp_offset = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	memcpy(chroma_qp_offset, &h->common.param.analyse.i_chroma_qp_offset, sizeof(int));
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);

    return 0;
}

int i264e_reconfig_trans_set(i264e_t *h, const void *param)
{
	int *chroma_qp_offset = (int *)param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	memcpy(&h->common.reconfig.i_chroma_qp_offset, chroma_qp_offset, sizeof(int));
	I264E_RCFG_TRANS_SET(h->common.reconfig.idr_reconfig_cnt);
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);

    return 0;
}

int i264e_reconfig_qpgmode_get(i264e_t *h, void *param)
{
    int *qpgmode = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	memcpy(qpgmode, &h->common.reconfig.qpgmode, sizeof(int));
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);

    return 0;
}

int i264e_reconfig_qpgmode_set(i264e_t *h, const void *param)
{
	int *qpgmode = (int *)param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	memcpy(&h->common.reconfig.qpgmode, qpgmode, sizeof(int));
	I264E_RCFG_QPGMODE_SET(h->common.reconfig.idr_reconfig_cnt);
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);

    return 0;
}

int i264e_reconfig_rcfile_set(i264e_t *h, const void *param)
{
	const RC_CFG_S_T21 *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	memcpy(&h->common.reconfig.rcfile, p, sizeof(RC_CFG_S_T21));
	h->common.reconfig.idr_rcfile_config_cnt = 1;
    i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
}

int i264e_reconfig_rcfile_get(i264e_t *h, void *param)
{
	RC_CFG_S_T21 *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	if((h->common.param.soc == C_T21) || (h->common.param.soc == C_T23)) {
		p->encProtocol                     = h->common.eprc_t21.cfg.encProtocol;
		p->gopMode                         = h->common.eprc_t21.cfg.gopMode;
		p->qpgMode                         = h->common.eprc_t21.cfg.qpgMode;
		p->u16FrmWidth                     = h->common.eprc_t21.cfg.u16FrmWidth;
		p->u16FrmHeight                    = h->common.eprc_t21.cfg.u16FrmHeight;
		p->u16MbWidth                      = h->common.eprc_t21.cfg.u16MbWidth;
		p->u16MbHeight                     = h->common.eprc_t21.cfg.u16MbHeight;
		p->fskMode                         = h->common.eprc_t21.cfg.fskMode;
		p->intelMode                       = h->common.eprc_t21.cfg.intelMode;
		p->priority                        = h->common.eprc_t21.cfg.priority;
		p->sceMode                         = h->common.eprc_t21.cfg.sceMode;
		p->superFrmCfg.superFrmMode        = h->common.eprc_t21.cfg.superFrmCfg.superFrmMode;
		p->superFrmCfg.u32SuperIFrmBitsThr = h->common.eprc_t21.cfg.superFrmCfg.u32SuperIFrmBitsThr;
		p->superFrmCfg.u32SuperPFrmBitsThr = h->common.eprc_t21.cfg.superFrmCfg.u32SuperPFrmBitsThr;
		p->superFrmCfg.u32SuperBFrmBitsThr = h->common.eprc_t21.cfg.superFrmCfg.u32SuperBFrmBitsThr;
		p->lostFrmCfg.bFrmLostEn           = h->common.eprc_t21.cfg.lostFrmCfg.bFrmLostEn;
		p->lostFrmCfg.frmLostMode          = h->common.eprc_t21.cfg.lostFrmCfg.frmLostMode;
		p->lostFrmCfg.u32FrmLostBpsThr     = h->common.eprc_t21.cfg.lostFrmCfg.u32FrmLostBpsThr;
		p->lostFrmCfg.u32FrmLostGaps       = h->common.eprc_t21.cfg.lostFrmCfg.u32FrmLostGaps;
		p->s8StartQp                       = h->common.eprc_t21.cfg.s8StartQp;
        {
            p->gopNormalP.s8IPQpDelta          = 3;
            p->gopSmartP.u32BgInterval         = h->common.param.i_gop * h->common.skip_h.mscenecnt;
            p->gopSmartP.s8BgQpDelta           = 3;
            p->gopSmartP.s8ViQpDelta           = 3;
            p->gopDoulP.u32SPInterval          = 0;
            p->gopDoulP.s8SPQpDelta            = 3;
            p->gopDoulP.s8IPQpDelta            = 3;
            p->gopBipredP.u32BFrmNum           = 1;
            p->gopBipredP.s8BQpDelta           = 3;
            p->gopBipredP.s8IPQpDelta          = 3;
        }
        switch(h->common.eprc_t21.cfg.gopMode) {
        case NORMALP:
            p->gopNormalP.s8IPQpDelta          = h->common.eprc_t21.cfg.gopNormalP.s8IPQpDelta;
            break;
        case SMARTP:
            p->gopSmartP.u32BgInterval         = h->common.eprc_t21.cfg.gopSmartP.u32BgInterval;
            p->gopSmartP.s8BgQpDelta           = h->common.eprc_t21.cfg.gopSmartP.s8BgQpDelta;
            p->gopSmartP.s8ViQpDelta           = h->common.eprc_t21.cfg.gopSmartP.s8ViQpDelta;
            break;
        case DOULP:
            p->gopDoulP.u32SPInterval          = h->common.eprc_t21.cfg.gopDoulP.u32SPInterval;
            p->gopDoulP.s8SPQpDelta            = h->common.eprc_t21.cfg.gopDoulP.s8SPQpDelta;
            p->gopDoulP.s8IPQpDelta            = h->common.eprc_t21.cfg.gopDoulP.s8IPQpDelta;
            break;
        case BIPREDP:
            p->gopBipredP.u32BFrmNum           = h->common.eprc_t21.cfg.gopBipredP.u32BFrmNum;
            p->gopBipredP.s8BQpDelta           = h->common.eprc_t21.cfg.gopBipredP.s8BQpDelta;
            p->gopBipredP.s8IPQpDelta          = h->common.eprc_t21.cfg.gopBipredP.s8IPQpDelta;
            break;
        default:
            break;
        }
		p->rcMode                          = h->common.eprc_t21.cfg.rcMode;
        {
            p->attrH264Cqp.u32Gop                  = h->common.param.i_gop;
            p->attrH264Cqp.u32SrcFrmRate           = h->common.param.i_fps_num;
            p->attrH264Cqp.frDstFrmRate            = h->common.param.i_fps_den;
            p->attrH264Cqp.u8IQp                   = h->common.param.rc.i_qp_constant - 3;
            p->attrH264Cqp.u8PQp                   = h->common.param.rc.i_qp_constant;

            p->attrH264Cbr.u32Gop                  = h->common.param.i_gop;
            p->attrH264Cbr.u32StatTime             = h->common.param.rc.i_static_time;
            p->attrH264Cbr.u32SrcFrmRate           = h->common.param.i_fps_num;
            p->attrH264Cbr.frDstFrmRate            = h->common.param.i_fps_den;
            p->attrH264Cbr.u32BitRate              = h->common.param.rc.i_bitrate;
            p->attrH264Cbr.u8FluctuateLevel        = 3;
            p->paramH264Cbr.u8MaxIprop             = 100;
            p->paramH264Cbr.u8MinIprop             = 1;
            p->paramH264Cbr.u8MaxReEncodeTimes     = h->common.param.i_fps_num / h->common.param.i_fps_den / 2;
            p->paramH264Cbr.u8MaxQp                = h->common.param.rc.i_qp_max;
            p->paramH264Cbr.u8MinQp                = h->common.param.rc.i_qp_min;
            p->paramH264Cbr.u8MaxIQp               = h->common.param.rc.i_qp_max;
            p->paramH264Cbr.u8MinIQp               = h->common.param.rc.i_qp_min;
            p->paramH264Cbr.s8IPQpDelta            = 3;
            p->paramH264Cbr.u8QualityLvl           = 3;
            p->paramH264Cbr.u8MaxIPQpDelta         = h->common.param.rc.i_gop_qp_step;
            p->paramH264Cbr.u8MaxPPQpDelta         = h->common.param.rc.i_qp_step;
            p->paramH264Cbr.s8IQpBias              = h->common.param.rc.i_biaslvl;

            p->attrH264Vbr.u32Gop                  = h->common.param.i_gop;
            p->attrH264Vbr.u32StatTime             = h->common.param.rc.i_static_time;
            p->attrH264Vbr.u32SrcFrmRate           = h->common.param.i_fps_num;
            p->attrH264Vbr.frDstFrmRate            = h->common.param.i_fps_den;
            p->attrH264Vbr.u32MaxBitRate           = h->common.param.rc.i_max_bitrate;
            p->attrH264Vbr.u8MaxQp                 = h->common.param.rc.i_qp_max;
            p->attrH264Vbr.u8MinQp                 = h->common.param.rc.i_qp_min;
            p->attrH264Vbr.u8MinIQp                = h->common.param.rc.i_qp_min;
            p->attrH264Vbr.s8AvbrEn                = 1;
            p->paramH264Vbr.u8ChangePos            = h->common.param.rc.i_change_pos;
            p->paramH264Vbr.u8MaxIprop             = 100;
            p->paramH264Vbr.u8MinIprop             = 1;
            p->paramH264Vbr.u8MaxReEncodeTimes     = h->common.param.i_fps_num / h->common.param.i_fps_den / 2;
            p->paramH264Vbr.s8IPQpDelta            = 3;
            p->paramH264Vbr.u8MaxIPQpDelta         = h->common.param.rc.i_gop_qp_step;
            p->paramH264Vbr.u8MaxPPQpDelta         = h->common.param.rc.i_qp_step;
            p->paramH264Vbr.u8QualityLvl           = h->common.param.rc.i_quality_level;
            p->paramH264Vbr.s8RQDelta              = 4;
            p->paramH264Vbr.s8IQpBias              = h->common.param.rc.i_biaslvl;

        }
        if (h->common.eprc_t21.cfg.rcMode == T21_CQP) {
            p->attrH264Cqp.u32Gop                  = h->common.eprc_t21.cfg.attrH264Cqp.u32Gop;
            p->attrH264Cqp.u32SrcFrmRate           = h->common.eprc_t21.cfg.attrH264Cqp.u32SrcFrmRate;
            p->attrH264Cqp.frDstFrmRate            = h->common.eprc_t21.cfg.attrH264Cqp.frDstFrmRate;
            p->attrH264Cqp.u8IQp                   = h->common.eprc_t21.cfg.attrH264Cqp.u8IQp;
            p->attrH264Cqp.u8PQp                   = h->common.eprc_t21.cfg.attrH264Cqp.u8PQp;
        }
        else if (h->common.eprc_t21.cfg.rcMode == T21_CBR) {
            p->attrH264Cbr.u32Gop                  = h->common.eprc_t21.cfg.attrH264Cbr.u32Gop;
            p->attrH264Cbr.u32StatTime             = h->common.eprc_t21.cfg.attrH264Cbr.u32StatTime;
            p->attrH264Cbr.u32SrcFrmRate           = h->common.eprc_t21.cfg.attrH264Cbr.u32SrcFrmRate;
            p->attrH264Cbr.frDstFrmRate            = h->common.eprc_t21.cfg.attrH264Cbr.frDstFrmRate;
            p->attrH264Cbr.u32BitRate              = h->common.eprc_t21.cfg.attrH264Cbr.u32BitRate;
            p->attrH264Cbr.u8FluctuateLevel        = h->common.eprc_t21.cfg.attrH264Cbr.u8FluctuateLevel;
            p->paramH264Cbr.u8MaxIprop             = h->common.eprc_t21.cfg.paramH264Cbr.u8MaxIprop;
            p->paramH264Cbr.u8MinIprop             = h->common.eprc_t21.cfg.paramH264Cbr.u8MinIprop;
            p->paramH264Cbr.u8MaxReEncodeTimes     = h->common.eprc_t21.cfg.paramH264Cbr.u8MaxReEncodeTimes;
            p->paramH264Cbr.u8MaxQp                = h->common.eprc_t21.cfg.paramH264Cbr.u8MaxQp;
            p->paramH264Cbr.u8MinQp                = h->common.eprc_t21.cfg.paramH264Cbr.u8MinQp;
            p->paramH264Cbr.u8MaxIQp               = h->common.eprc_t21.cfg.paramH264Cbr.u8MaxIQp;
            p->paramH264Cbr.u8MinIQp               = h->common.eprc_t21.cfg.paramH264Cbr.u8MinIQp;
            p->paramH264Cbr.s8IPQpDelta            = h->common.eprc_t21.cfg.paramH264Cbr.s8IPQpDelta;
            p->paramH264Cbr.u8QualityLvl           = h->common.eprc_t21.cfg.paramH264Cbr.u8QualityLvl;
            p->paramH264Cbr.u8MaxIPQpDelta         = h->common.eprc_t21.cfg.paramH264Cbr.u8MaxIPQpDelta;
            p->paramH264Cbr.u8MaxPPQpDelta         = h->common.eprc_t21.cfg.paramH264Cbr.u8MaxPPQpDelta;
            p->paramH264Cbr.s8IQpBias              = h->common.eprc_t21.cfg.paramH264Cbr.s8IQpBias;

        }
        else if (h->common.eprc_t21.cfg.rcMode == T21_VBR || h->common.eprc_t21.cfg.rcMode == T21_SMART) {
            p->attrH264Vbr.u32Gop                  = h->common.eprc_t21.cfg.attrH264Vbr.u32Gop;
            p->attrH264Vbr.u32StatTime             = h->common.eprc_t21.cfg.attrH264Vbr.u32StatTime;
            p->attrH264Vbr.u32SrcFrmRate           = h->common.eprc_t21.cfg.attrH264Vbr.u32SrcFrmRate;
            p->attrH264Vbr.frDstFrmRate            = h->common.eprc_t21.cfg.attrH264Vbr.frDstFrmRate;
            p->attrH264Vbr.u32MaxBitRate           = h->common.eprc_t21.cfg.attrH264Vbr.u32MaxBitRate;
            p->attrH264Vbr.u8MaxQp                 = h->common.eprc_t21.cfg.attrH264Vbr.u8MaxQp;
            p->attrH264Vbr.u8MinQp                 = h->common.eprc_t21.cfg.attrH264Vbr.u8MinQp;
            p->attrH264Vbr.u8MinIQp                = h->common.eprc_t21.cfg.attrH264Vbr.u8MinIQp;
            p->attrH264Vbr.s8AvbrEn                = h->common.eprc_t21.cfg.attrH264Vbr.s8AvbrEn;
            p->paramH264Vbr.u8ChangePos            = h->common.eprc_t21.cfg.paramH264Vbr.u8ChangePos;
            p->paramH264Vbr.u8MaxIprop             = h->common.eprc_t21.cfg.paramH264Vbr.u8MaxIprop;
            p->paramH264Vbr.u8MinIprop             = h->common.eprc_t21.cfg.paramH264Vbr.u8MinIprop;
            p->paramH264Vbr.u8MaxReEncodeTimes     = h->common.eprc_t21.cfg.paramH264Vbr.u8MaxReEncodeTimes;
            p->paramH264Vbr.s8IPQpDelta            = h->common.eprc_t21.cfg.paramH264Vbr.s8IPQpDelta;
            p->paramH264Vbr.u8MaxIPQpDelta         = h->common.eprc_t21.cfg.paramH264Vbr.u8MaxIPQpDelta;
            p->paramH264Vbr.u8MaxPPQpDelta         = h->common.eprc_t21.cfg.paramH264Vbr.u8MaxPPQpDelta;
            p->paramH264Vbr.u8QualityLvl           = h->common.eprc_t21.cfg.paramH264Vbr.u8QualityLvl;
            p->paramH264Vbr.s8RQDelta              = h->common.eprc_t21.cfg.paramH264Vbr.s8RQDelta;
            p->paramH264Vbr.s8IQpBias              = h->common.eprc_t21.cfg.paramH264Vbr.s8IQpBias;

        }
        {
            p->rcInside.IQpMode                = h->common.eprc_t21.cfg.rcInside.IQpMode;
            p->rcInside.fstPQpMode             = h->common.eprc_t21.cfg.rcInside.fstPQpMode;
            p->rcInside.avgPQpMode             = h->common.eprc_t21.cfg.rcInside.avgPQpMode;
            p->rcInside.qpModel                = h->common.eprc_t21.cfg.rcInside.qpModel;
            p->rcInside.bitrLvlEn              = h->common.eprc_t21.cfg.rcInside.bitrLvlEn;
            p->rcInside.sceJudgeLvl            = h->common.eprc_t21.cfg.rcInside.sceJudgeLvl;
            p->rcInside.sceJudgeEn             = h->common.eprc_t21.cfg.rcInside.sceJudgeEn;
            p->rcInside.useFstPEn              = h->common.eprc_t21.cfg.rcInside.useFstPEn;
            p->rcInside.avgQpNewEn             = h->common.eprc_t21.cfg.rcInside.avgQpNewEn;
            p->rcInside.avgPQpNum              = h->common.eprc_t21.cfg.rcInside.avgPQpNum;
            p->rcInside.qpFluctEn              = h->common.eprc_t21.cfg.rcInside.qpFluctEn;
            p->rcInside.qpFluctLvl             = h->common.eprc_t21.cfg.rcInside.qpFluctLvl;
            p->rcInside.fluctLvl               = h->common.eprc_t21.cfg.rcInside.fluctLvl;
            p->rcInside.bndEn                  = h->common.eprc_t21.cfg.rcInside.bndEn;
            p->rcInside.BndLvl                 = h->common.eprc_t21.cfg.rcInside.BndLvl;
            p->rcInside.stmLvl                 = h->common.eprc_t21.cfg.rcInside.stmLvl;
            p->rcInside.t0Method               = h->common.eprc_t21.cfg.rcInside.t0Method;
            p->rcInside.avgPBitsNum            = h->common.eprc_t21.cfg.rcInside.avgPBitsNum;
            p->rcInside.gammaEn                = h->common.eprc_t21.cfg.rcInside.gammaEn;
            p->rcInside.stillSize              = h->common.eprc_t21.cfg.rcInside.stillSize;
            p->rcInside.moveSize               = h->common.eprc_t21.cfg.rcInside.moveSize;
            p->rcInside.qpgIMode               = h->common.eprc_t21.cfg.rcInside.qpgIMode;
            p->rcInside.qpgPMode               = h->common.eprc_t21.cfg.rcInside.qpgPMode;
        }
        {
            /* motion */
            p->siParamH264.frm_re0A                    = h->common.eprc_t21.cfg.siParamH264.frm_re0A;
            p->siParamH264.frm_re1A                    = h->common.eprc_t21.cfg.siParamH264.frm_re1A;
            p->siParamH264.frm_re2A                    = h->common.eprc_t21.cfg.siParamH264.frm_re2A;
            p->siParamH264.frm_re3A                    = h->common.eprc_t21.cfg.siParamH264.frm_re3A;
            p->siParamH264.scl                         = h->common.eprc_t21.cfg.siParamH264.scl;
            p->siParamH264.hpel_en                     = h->common.eprc_t21.cfg.siParamH264.hpel_en;
            p->siParamH264.qpel_en                     = h->common.eprc_t21.cfg.siParamH264.qpel_en;
            p->siParamH264.max_sech_step_i             = h->common.eprc_t21.cfg.siParamH264.max_sech_step_i;
            p->siParamH264.max_mvrx_i                  = h->common.eprc_t21.cfg.siParamH264.max_mvrx_i;
            p->siParamH264.max_mvry_i                  = h->common.eprc_t21.cfg.siParamH264.max_mvry_i;
            p->siParamH264.fs_en                       = h->common.eprc_t21.cfg.siParamH264.fs_en;
            p->siParamH264.fs_md                       = h->common.eprc_t21.cfg.siParamH264.fs_md;
            p->siParamH264.fs_px                       = h->common.eprc_t21.cfg.siParamH264.fs_px;
            p->siParamH264.fs_py                       = h->common.eprc_t21.cfg.siParamH264.fs_py;
            p->siParamH264.fs_rx                       = h->common.eprc_t21.cfg.siParamH264.fs_rx;
            p->siParamH264.fs_ry                       = h->common.eprc_t21.cfg.siParamH264.fs_ry;
            p->siParamH264.frm_mv_en                   = h->common.eprc_t21.cfg.siParamH264.frm_mv_en;
            p->siParamH264.frm_mv_size                 = h->common.eprc_t21.cfg.siParamH264.frm_mv_size;
            p->siParamH264.glb_mv_en                   = h->common.eprc_t21.cfg.siParamH264.glb_mv_en;
            p->siParamH264.me_step_en                  = h->common.eprc_t21.cfg.siParamH264.me_step_en;
            p->siParamH264.me_step_0                   = h->common.eprc_t21.cfg.siParamH264.me_step_0;
            p->siParamH264.me_step_1                   = h->common.eprc_t21.cfg.siParamH264.me_step_1;
            /* transform & quant & loop filter */
            p->siParamH264.deadzone0Aiy4               = h->common.eprc_t21.cfg.siParamH264.deadzone0Aiy4;
            p->siParamH264.deadzone1Apy4               = h->common.eprc_t21.cfg.siParamH264.deadzone1Apy4;
            p->siParamH264.deadzone2Aic4               = h->common.eprc_t21.cfg.siParamH264.deadzone2Aic4;
            p->siParamH264.deadzone3Apc4               = h->common.eprc_t21.cfg.siParamH264.deadzone3Apc4;
            p->siParamH264.deadzone4Aiy8               = h->common.eprc_t21.cfg.siParamH264.deadzone4Aiy8;
            p->siParamH264.deadzone5Apy8               = h->common.eprc_t21.cfg.siParamH264.deadzone5Apy8;
            p->siParamH264.deadzone5Apy8               = h->common.eprc_t21.cfg.siParamH264.deadzone5Apy8;
            p->siParamH264.deadzone7Aicdc              = h->common.eprc_t21.cfg.siParamH264.deadzone7Aicdc;
            p->siParamH264.deadzone8Apcdc              = h->common.eprc_t21.cfg.siParamH264.deadzone8Apcdc;
            p->siParamH264.acmask_mode                 = h->common.eprc_t21.cfg.siParamH264.acmask_mode;
            p->siParamH264.deblock                     = h->common.eprc_t21.cfg.siParamH264.deblock;
            p->siParamH264.alpha_c0_offset             = h->common.eprc_t21.cfg.siParamH264.alpha_c0_offset;
            p->siParamH264.beta_offset                 = h->common.eprc_t21.cfg.siParamH264.beta_offset;
            /* mode decision */
            p->siParamH264.jm_lambda2_en               = h->common.eprc_t21.cfg.siParamH264.jm_lambda2_en;
            p->siParamH264.inter_nei_en                = h->common.eprc_t21.cfg.siParamH264.inter_nei_en;
            p->siParamH264.skip_bias_en                = h->common.eprc_t21.cfg.siParamH264.skip_bias_en;
            p->siParamH264.dcm_en                      = h->common.eprc_t21.cfg.siParamH264.dcm_en;
            p->siParamH264.dcm_param                   = h->common.eprc_t21.cfg.siParamH264.dcm_param;
            p->siParamH264.use_intra_in_pframe         = h->common.eprc_t21.cfg.siParamH264.use_intra_in_pframe;
            p->siParamH264.cqp_offset                  = h->common.eprc_t21.cfg.siParamH264.cqp_offset;
            p->siParamH264.mb_mode_useMASK             = h->common.eprc_t21.cfg.siParamH264.mb_mode_useMASK;
            /* eigen enable bool */
            p->siParamH264.sas_eigen_en                = h->common.eprc_t21.cfg.siParamH264.sas_eigen_en;
            p->siParamH264.crp_eigen_en                = h->common.eprc_t21.cfg.siParamH264.crp_eigen_en;
            /* refresh */
            p->siParamH264.refresh_enMASK              = h->common.eprc_t21.cfg.siParamH264.refresh_enMASK;
            p->siParamH264.refresh_mode                = h->common.eprc_t21.cfg.siParamH264.refresh_mode;
            p->siParamH264.refresh_bias                = h->common.eprc_t21.cfg.siParamH264.refresh_bias;
            p->siParamH264.refresh_cplx_thd            = h->common.eprc_t21.cfg.siParamH264.refresh_cplx_thd;
            /* force i16dc */
            p->siParamH264.force_i16dc                 = h->common.eprc_t21.cfg.siParamH264.force_i16dc;
            p->siParamH264.i16dc_cplx_thdIDR           = h->common.eprc_t21.cfg.siParamH264.i16dc_cplx_thdIDR;
            p->siParamH264.i16dc_cplx_thdELSE          = h->common.eprc_t21.cfg.siParamH264.i16dc_cplx_thdELSE;
            p->siParamH264.i16dc_qp_base               = h->common.eprc_t21.cfg.siParamH264.i16dc_qp_base;
            p->siParamH264.i16dc_qp_sel                = h->common.eprc_t21.cfg.siParamH264.i16dc_qp_sel;
            /* force i16 */
            p->siParamH264.force_i16                   = h->common.eprc_t21.cfg.siParamH264.force_i16;
            p->siParamH264.i16_qp_baseIDR              = h->common.eprc_t21.cfg.siParamH264.i16_qp_baseIDR;
            p->siParamH264.i16_qp_baseELSE             = h->common.eprc_t21.cfg.siParamH264.i16_qp_baseELSE;
            p->siParamH264.i16_qp_sel                  = h->common.eprc_t21.cfg.siParamH264.i16_qp_sel;
            /* rrs */
            p->siParamH264.rrs_enMASK                  = h->common.eprc_t21.cfg.siParamH264.rrs_enMASK;
            p->siParamH264.rrs_uv_enMASK               = h->common.eprc_t21.cfg.siParamH264.rrs_uv_enMASK;
            p->siParamH264.rrs_size_y                  = h->common.eprc_t21.cfg.siParamH264.rrs_size_y;
            p->siParamH264.rrs_size_c                  = h->common.eprc_t21.cfg.siParamH264.rrs_size_c;
            /* skin detection */
            p->siParamH264.skin_dt_en                  = h->common.eprc_t21.cfg.siParamH264.skin_dt_en;
            p->siParamH264.skin_lvl                    = h->common.eprc_t21.cfg.siParamH264.skin_lvl;
            p->siParamH264.skin_cnt_thd                = h->common.eprc_t21.cfg.siParamH264.skin_cnt_thd;
        }

	} else {
		return -1;
	}
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_rc_set(i264e_t *h, const void *param)
{
	const i264e_rcfg_rc_param_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	h->common.reconfig.rc.rc_method = p->rc_method;
	if (p->rc_method == I264E_RC_CQP) {
		h->common.reconfig.rc.qp = c_clip3(p->qp, 0, QP_MAX);
		float qp_p = h->common.reconfig.rc.qp;
		float qp_i = qp_p - 6*log2f(h->common.param.rc.f_ip_factor);
		float qp_b = qp_p + 6*log2f(h->common.param.rc.f_pb_factor);
		h->common.reconfig.rc.qp_min = c_clip3((int)(C_MIN3(qp_p, qp_i, qp_b)), 0, QP_MAX);
		h->common.reconfig.rc.qp_max = c_clip3((int)(C_MAX3(qp_p, qp_i, qp_b) + .999),0, QP_MAX);
	} else if (p->rc_method == I264E_RC_CBR) {
		h->common.reconfig.rc.qp_min = c_clip3(p->min_qp, 1, QP_MAX);
		h->common.reconfig.rc.qp_max = c_clip3(p->max_qp, 1, QP_MAX);
		h->common.reconfig.rc.bitrate = p->bitrate;
		h->common.reconfig.rc.biaslvl = c_clip3(p->biaslvl, -10, 10);
        h->common.reconfig.rc.qp_step = C_MAX(p->qp_step, 0);
		h->common.reconfig.rc.gop_qp_step = C_MAX(p->gop_qp_step, 0);
		h->common.reconfig.rc.adaptive_mode = !!p->adaptive_mode;
		h->common.reconfig.rc.gop_relation = !!p->gop_relation;
	} else if (p->rc_method == I264E_RC_VBR) {
		h->common.reconfig.rc.qp_min = c_clip3(p->min_qp, 1, QP_MAX);
		h->common.reconfig.rc.qp_max = c_clip3(p->max_qp, 1, QP_MAX);
		h->common.reconfig.rc.static_time = C_MAX(p->static_time, 1);
		h->common.reconfig.rc.max_bitrate = C_MAX(p->max_bitrate, 128);
		h->common.reconfig.rc.biaslvl = c_clip3(p->biaslvl, -10, 10);
		h->common.reconfig.rc.change_pos = c_clip3(p->change_pos, 0, 100);
		h->common.reconfig.rc.quality_level = c_clip3(p->quality_level, 0, 6);
		h->common.reconfig.rc.qp_step = C_MAX(p->qp_step, 0);
		h->common.reconfig.rc.gop_qp_step = C_MAX(p->gop_qp_step, 0);
		h->common.reconfig.rc.gop_relation = !!p->gop_relation;
	} else if (p->rc_method == I264E_RC_SMART) {
		h->common.reconfig.rc.qp_min = c_clip3(p->min_qp, 1, QP_MAX);
		h->common.reconfig.rc.qp_max = c_clip3(p->max_qp, 1, QP_MAX);
		h->common.reconfig.rc.static_time = C_MAX(p->static_time, 1);
		h->common.reconfig.rc.max_bitrate = C_MAX(p->max_bitrate, 128);
		h->common.reconfig.rc.biaslvl = c_clip3(p->biaslvl, -10, 10);
		h->common.reconfig.rc.change_pos = c_clip3(p->change_pos, 0, 100);
		h->common.reconfig.rc.quality_level = c_clip3(p->quality_level, 0, 6);
		h->common.reconfig.rc.qp_step = C_MAX(p->qp_step, 0);
		h->common.reconfig.rc.gop_qp_step = C_MAX(p->gop_qp_step, 0);
		h->common.reconfig.rc.gop_relation = !!p->gop_relation;
	} else {
		i264e_log(h, C_LOG_ERROR, "unsupport rc_method = %d\n", p->rc_method);
		i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
		return -1;
	}

	I264E_RCFG_RC_SET(h->common.reconfig.idr_reconfig_cnt);
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_rc_get(i264e_t *h, void *param)
{
	i264e_rcfg_rc_param_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	p->rc_method = h->common.reconfig.rc.rc_method;
	p->qp = h->common.reconfig.rc.qp;
	p->max_qp = h->common.reconfig.rc.qp_max;
	p->min_qp = h->common.reconfig.rc.qp_min;
	p->bitrate = h->common.reconfig.rc.bitrate;
	p->biaslvl = h->common.reconfig.rc.biaslvl;
	p->qp_step = h->common.reconfig.rc.qp_step;
	p->gop_qp_step = h->common.reconfig.rc.gop_qp_step;
	p->adaptive_mode = h->common.reconfig.rc.adaptive_mode;
	p->gop_relation = h->common.reconfig.rc.gop_relation;
	p->static_time = h->common.reconfig.rc.static_time;
	p->max_bitrate = h->common.reconfig.rc.max_bitrate;
	p->change_pos = h->common.reconfig.rc.change_pos;
	p->quality_level = h->common.reconfig.rc.quality_level;

	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);

	return 0;
}

int i264e_reconfig_rc_trig_set(i264e_t *h, const void *param)
{
	const i264e_rcfg_rc_trig_param_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	I264E_RCFG_RC_TRIG_SET(h->common.reconfig.idr_reconfig_cnt);
	h->common.reconfig.rc.trigger_level = p->trigger_level;
	h->common.reconfig.rc.new_qp_max_thr = p->new_qp_max_thr;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_rc_trig_get(i264e_t *h, void *param)
{
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	i264e_rcfg_rc_trig_param_t *p = param;
	p->trigger_level = h->common.reconfig.rc.trigger_level;
	p->new_qp_max_thr = h->common.reconfig.rc.new_qp_max_thr;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}


int i264e_reconfig_fps_set(i264e_t *h, const void *param)
{
	const i264e_rcfg_fps_param_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	if (!p->i_fps_num || !p->i_fps_den) {
		i264e_log(h, C_LOG_ERROR, "reconfig fps param error! i_fps_num = %u, i_fps_den = %u\n", p->i_fps_num, p->i_fps_den);
		i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
		return -1;
	}
	h->common.reconfig.i_fps_num = p->i_fps_num;
	h->common.reconfig.i_fps_den = p->i_fps_den;
	I264E_RCFG_FPS_SET(h->common.reconfig.idr_reconfig_cnt);
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_fps_get(i264e_t *h, void *param)
{
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	i264e_rcfg_fps_param_t *p = param;
	p->i_fps_num = h->common.reconfig.i_fps_num;
	p->i_fps_den = h->common.reconfig.i_fps_den;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_enidr_set(i264e_t *h, const void *param)
{
	const i264e_rcfg_enidr_param_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	I264E_RCFG_ENIDR_SET(h->common.reconfig.idr_reconfig_cnt);
	h->common.reconfig.enable_enidr = p->on;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_enidr_get(i264e_t *h, void *param)
{
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	i264e_rcfg_enidr_param_t *p = param;
	p->on = h->common.reconfig.enable_enidr;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_gop_set(i264e_t *h, const void *param)
{
	const i264e_rcfg_gop_param_t *p = param;
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	I264E_RCFG_GOP_SET(h->common.reconfig.idr_reconfig_cnt);
	h->common.reconfig.gopsizebak = p->gopsize;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}

int i264e_reconfig_gop_get(i264e_t *h, void *param)
{
	i264e_pthread_mutex_lock(&h->common.reconfig.mutex_rcfg);
	i264e_rcfg_gop_param_t *p = param;
	p->gopsize = h->common.reconfig.gopsizebak;
	i264e_pthread_mutex_unlock(&h->common.reconfig.mutex_rcfg);
	return 0;
}
